﻿using System.Windows;
using System.Windows.Controls;

namespace WpfAnimationDemo.Controls.WaferRobotV2
{
    public class WaferRobotControl : Control
    {
        static WaferRobotControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WaferRobotControl), new FrameworkPropertyMetadata(typeof(WaferRobotControl)));
        }

        public static readonly DependencyProperty WaferStatusProperty = DependencyProperty.Register(nameof(WaferStatus), typeof(int), typeof(WaferRobotControl));
        public int WaferStatus { get => (int)GetValue(WaferStatusProperty); set => SetValue(WaferStatusProperty, value); }

        public static readonly DependencyProperty RobotZActionProperty = DependencyProperty.Register(
           nameof(RobotZAction), typeof(WaferRobotZAction), typeof(WaferRobotControl),
           new PropertyMetadata(WaferRobotZAction.Z_Origin, RobotZActionPropertyChangedCallback));
        /// <summary>
        /// Z 轴动作
        /// </summary>
        public WaferRobotZAction RobotZAction
        {
            get => (WaferRobotZAction)GetValue(RobotZActionProperty);
            set => SetValue(RobotZActionProperty, value);
        }

        /// <summary>
        /// Z 轴动作变化
        /// </summary>
        private static void RobotZActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as WaferRobotControl;
            if (control == null) return;

            var oldAct = (WaferRobotZAction)e.OldValue;
            var newAct = (WaferRobotZAction)e.NewValue;
            if (newAct != oldAct)
            {
                VisualStateManager.GoToState(control, newAct.ToString(), true);
            }
        }

        public static readonly DependencyProperty RobotXActionProperty = DependencyProperty.Register(
            nameof(RobotXAction), typeof(WaferRobotXAction), typeof(WaferRobotControl),
            new PropertyMetadata(WaferRobotXAction.X_Origin, RobotXActionPropertyChangedCallback));
        /// <summary>
        /// X 轴动作
        /// </summary>
        public WaferRobotXAction RobotXAction
        {
            get => (WaferRobotXAction)GetValue(RobotXActionProperty);
            set => SetValue(RobotXActionProperty, value);
        }

        /// <summary>
        /// X 轴动作变化
        /// </summary>
        private static void RobotXActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as WaferRobotControl;
            if (control == null) return;

            var oldAct = (WaferRobotXAction)e.OldValue;
            var newAct = (WaferRobotXAction)e.NewValue;
            if (newAct != oldAct)
            {
                VisualStateManager.GoToState(control, newAct.ToString(), true);
            }
        }

        public static readonly DependencyProperty RobotTActionProperty = DependencyProperty.Register(
            nameof(RobotTAction), typeof(WaferRobotTAction), typeof(WaferRobotControl),
            new PropertyMetadata(WaferRobotTAction.T_Origin, RobotTActionPropertyChangedCallback));
        /// <summary>
        /// T 轴动作
        /// </summary>
        public WaferRobotTAction RobotTAction
        {
            get => (WaferRobotTAction)GetValue(RobotTActionProperty);
            set => SetValue(RobotTActionProperty, value);
        }

        /// <summary>
        /// T 轴动作变化
        /// </summary>
        private static void RobotTActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as WaferRobotControl;
            if (control == null) return;

            var oldAct = (WaferRobotTAction)e.OldValue;
            var newAct = (WaferRobotTAction)e.NewValue;
            if (newAct != oldAct)
            {
                VisualStateManager.GoToState(control, newAct.ToString(), true);
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            VisualStateManager.GoToState(this, WaferRobotZAction.Z_Origin.ToString(), true);
            VisualStateManager.GoToState(this, WaferRobotXAction.X_Origin.ToString(), true);
            VisualStateManager.GoToState(this, WaferRobotTAction.T_Origin.ToString(), true);
        }
    }
}
